<?php
namespace app\console\controller;
use app\model\Mysql as SelfModel;
use think\facade\Request;
use think\facade\Route;
use think\facade\View;
use think\facade\Db;
class Mysql extends Base
{


    /**
     正则替换
     */
    public function replace()
    {
        $sql = "update tp_web_article set title=replace(title,'.jpg','')";
        $article_sql = "update tp_web_article set title=replace(single_pic,'http://47.111.252.1:808','')";
        Db::execute($article_sql);
        $article_sql2 = "update tp_web_article set title=replace(video_url,'http://47.111.252.1:808','')";
        Db::execute($article_sql2);
        $banner_sql = "update tp_web_article set title=replace(single_pic,'http://47.111.252.1:808','')";
        Db::execute($banner_sql);



        echo 'ok';
    }

    /**
     * 添加
     * @return mixed
     */
    public function create()
    {

        if ($this->request->isPost()) {
            $param = $this->request->param();
            $ret = (new SelfModel())->save($param);
            if ($ret) {
                win('操作成功', Route::buildUrl('index'));
            }
            fail('操作失败');
        }

        return View::fetch('', ['info' => []]);
    }

    /**
     * 修改
     * @param $id
     * @return mixed
     */
    public function update($id)
    {
        $param = $this->request->param();
        $info = SelfModel::find($id);
        if ($this->request->isPost()) {
            $ret =  $info->save($param,$id);
            if ($ret !== false) {
                win('修改成功', Route::buildUrl('index'));
            }
            fail('修改失败');
        }


        return View::fetch('create', ['info' => $info]);
    }

    /**
     * 删除方法
     * @param $id
     */
    public function destory($id)
    {
        if ($this->request->isGet()) {
            $ret = SelfModel::destroy($id,config('soft_delete'));
            if ($ret) jump('删除成功', Route::buildUrl('index'));
            fail('删除失败');
        }
    }

    /**
     * 软删除数据恢复方法
     * @param $id
     */
    public function restore($id)
    {
        $user = SelfModel::onlyTrashed()->find($id);
        $ret = $user->restore();
        if ($ret === true) {
            jump('恢复成功', Route::buildUrl('index'));
        }
    }

    /**
     * 公共切换状态
     * @return bool|int
     */
    public function check()
    {
        $param = $this->request->param();
        $ret = (new SelfModel())->where('id', $param['id'])->value($param['check']) == '1' ? 2 : 1;
        if ((new SelfModel())->where('id', $param['id'])->update([$param['check'] => $ret])) {
            return $ret;
        }
        return false;
    }


    public function ab(){
        \think\facade\Hook::listen('loginRemember');
        return 2;

    }

    /**
     * 首页批量更新
     */
    public function index_update()
    {
        $param = $this->request->param();
        $data = [];
        if(!empty($param['sort'])){
            foreach ($param['sort'] as $k => $v) {
                $data[$k]['id'] = $k;
                $data[$k]['sort'] = $v;
                // $ret = Db::name($this->controller)->where('id',$k)->setField('sort',$v);
            }

        }
        $ret = (new SelfModel())->allowField(['id','sort'])->saveAll($data);
        if ($ret !== false) {
            jump('更新成功', Route::buildUrl('index'));
        }
    }



}